import router from './router'
import route from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import { isRelogin } from '@/utils/request'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/auth-redirect', '/bind', '/register']

router.beforeEach((to, from, next) => {
  NProgress.start()
  //只要切换路由就更新缺陷
  store.dispatch('RefreshDefect')



  if (getToken()) {


    if(store.getters.allDefectObj['HR-WEB-054'] == 0  && to.meta.title == '退休管理'){
      store.dispatch('settings/setTitle', '退休')
    }
    else if(store.getters.allDefectObj['HR-WEB-059'] == 0  && to.meta.title == '岗位调整'){
      store.dispatch('settings/setTitle', '调整')
    }
    else if(store.getters.allDefectObj['HR-WEB-064'] == 0  && to.meta.title == '请假登记'){
      store.dispatch('settings/setTitle', '登记')
    }
    else if(store.getters.allDefectObj['HR-WEB-152'] == 0  && to.meta.title == '个人信息维护'){
      store.dispatch('settings/setTitle', '员工个人信息')
    }
    else if(store.getters.allDefectObj['HR-WEB-171'] == 0  && to.meta.title == '个人奖励信息'){
      store.dispatch('settings/setTitle', '个人惩罚信息')
    }
    else if(store.getters.allDefectObj['HR-WEB-021'] == 0  && to.meta.title == '部门管理'){
      store.dispatch('settings/setTitle', '管理部门')
    }
    else if( to.meta.title == '薪资基表管理'){
      store.dispatch('settings/setTitle', '当月薪资基表管理')
    }

    
    
    else{
      to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
    }
 
    /* has token*/
    if (to.path === '/login') {



    
      next({ path: '/' })
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        isRelogin.show = true
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(() => {
          isRelogin.show = false
          store.dispatch('GenerateRoutes').then(accessRoutes => {

            console.log('accessRoutes', accessRoutes)
            let tempAccessRoutes = accessRoutes; 
    

            // 根据roles权限生成可访问的路由表
            router.addRoutes(tempAccessRoutes) // 动态添加可访问路由表
            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
          })
        }).catch(err => {
            store.dispatch('LogOut').then(() => {
              Message.error(err)
              next({ path: '/' })
            })
          })
      } else {
      
            next()
      
          


      }
    }
  } else {

    if(to.fullPath.indexOf("?") !=-1){
      let tempUlr = decodeURIComponent(to.fullPath)
      console.log(tempUlr,"tempUlr")
      let params = to.fullPath.match(/\?(.*)/)[1]
      //替换params中的%20为+
     
      params = decodeURIComponent(params)
      params = params.replace(' ',"+")
   
     //params存储到浏览器session 使用store的SET_SPPARAMS方法 user.js下的
      
  if(params){     store.commit('SET_SPPARAMS',params) }
     }

      console.log(to.path)
      if(to.path.indexOf("/chat")==0){
          // 即时通讯地址，免登录
          next()
      }else{
          // 没有token
          if (whiteList.indexOf(to.path) !== -1) {
              // 在免登录白名单，直接进入
              next()
          } else {
              // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
            //使用正则取出 ?= 后面的字符串 
            if(to.fullPath.indexOf("?") !=-1){

           let params = to.fullPath.match(/\?(.*)/)[1]
            //替换params中的%20为+
           
            params = decodeURIComponent(params)
            params = params.replace(' ',"+")
          
            //params存储到浏览器session 使用store的SET_SPPARAMS方法 user.js下的
             
         if(params){     store.commit('SET_SPPARAMS',params) }
            }
              next({path:'/login'})


            
              NProgress.done()
          }
      }
  }
})

router.afterEach(() => {
  NProgress.done()
})
